package com.bootdo.system.config;

import java.io.IOException;
import java.util.Arrays;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CorsFilter implements Filter {
	// 跨域域名设置
	public static final String[] ALLOW_DOMAIN = { "http://mos.suyongw.com",
	            "http://192.168.1.142:8080" };
	     public void init(FilterConfig filterConfig) throws ServletException {
	      		}
	 
	     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
	         throws IOException, ServletException {	    	 
	         HttpServletResponse httpResponse = (HttpServletResponse) response;
	         HttpServletRequest req = (HttpServletRequest) request;
	         String originHeader = req.getHeader("Origin");
	         if (Arrays.asList(ALLOW_DOMAIN).contains(originHeader)) {
	         httpResponse.setHeader("Access-Control-Allow-Origin",originHeader);//设置允许跨域的域名，需要发送cookie信息，所以此处需要指定具体的域名，
	         httpResponse.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
	         httpResponse.setHeader("Access-Control-Allow-Methods", "GET, PUT, DELETE, POST");//允许跨域的请求方式
	         /**
	          * ajax请求的时候如果带有xhrFields:{withCredentials:true}，
	         * 那么服务器后台在配置跨域的时候就必须要把Access-Control-Allow-Credentials这个请求头加上去
	         */
	         httpResponse.setHeader("Access-Control-Allow-Credentials", "true");//允许发送Cookie信息
	         httpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // 支持HTTP 1.1.
	         httpResponse.setHeader("Pragma", "no-cache"); // 支持HTTP 1.0. response.setHeader("Expires", "0");
	         }
	         chain.doFilter(request, response);
	     }
	 
	     public void destroy() {
	     
	     }
	 }     